let rec power x n = 
match n with
0 -> 1
|n when n > 0 -> n * power x (n-1)
|_-> invalid_arg " n inferieur a 0 ";;


let rec application l x =
 match l with
    []->0
   |(c,d)::l -> c*(power x p) + application l x ;;



let rec add poly1 poly2 = 
 match (poly1, poly2) with
  ([],[]) -> []
  |(p1,[]) -> p1
  |([],p2) -> p2
  |((c1,d1)::p1,(c2,d2)::p2) -> 
   if d1 = d2 then 
    if c1 + c2 <> 0 then (c1+c2,d2):: add p1 p2 
    else add poly1 poly2
   else if d1 < d2 then (c1,d1) :: add p1 ((c2,d2)::p2)
   else (c2,d2) :: add ((c1,d1)::p1) p2 ;;  
   
let rec soustract poly1 poly2 = 
 match (poly1, poly2) with
  ([],[]) -> []
  |(p1,[]) -> p1
  |([],(a,b)::p2) -> (-a,b) :: soustract [] p2
  |((c1,d1)::p1,(c2,d2)::p2) -> 
   if d1 = d2 then 
    if c1 - c2 <> 0 then (c1-c2,d2):: add p1 p2 
    else add poly1 poly2
   else if d1 < d2 then (c1,d1) :: add p1 ((c2,d2)::p2)
   else (0-c2,d2) :: add ((c1,d1)::p1) p2 ;;

let rec deriv list = match list with 
[] ->[]
|(e1,e2)::list -> (e1*e2,e2-1)::deriv list;;

power 5 2;;
application [(2,2);(2,3);(4,4)] 2;;
add [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;
soustract [(1,2);(2,6);(7,8);(2,12)] [(1,4);(3,6);(1,9)];;
deriv [(2,2);(2,3);(4,4)];;

